import login from "../views/login.art";
import router from "../router/router.js";
import ajax from "../business/ajax.js";
function loginInit(res) {
  res.render(login());
  $("#registerBn").on("click", clickHandler);
  $("#loginForm").on("submit", submitHandler);
  $(document).on("login", loginHandler);
}

function clickHandler(e) {
  router.go("/register");
}

function submitHandler(e) {
  e.preventDefault();
  var formData = new FormData(this);
  ajax("login", formData);
}
function loginHandler(e, data) {
  $("#userName").text(data.user);
  $("#logout").css("display", "block");
  $("#listMenu").css("display", "block");
  if (data.result) {
    router.go("/list", data.userList);
  }
}

export default loginInit;
